package com.zgh.controller;

import com.alibaba.fastjson.JSON;
import com.zgh.domain.Group;
import com.zgh.domain.Result;
import com.zgh.domain.UserMatch;
import com.zgh.service.UserMatchService;
import com.zgh.util.QiniuUtil;
import com.zgh.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/userMatch")
public class UserMatchController {
    @Autowired
    private UserMatchService userMatchService;

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result getAllUserMatch(){
        List<UserMatch> userMatchList = userMatchService.findAll();
        return new Result(true,"查询成功",userMatchList);
    }

    @RequestMapping(value = "/findByNativeAndRegion",method = RequestMethod.POST)
    public Result findUserByNativeAndRegion(@RequestBody String params){
        //所在地
        String position = JSON.parseObject(params).getString("position");
        //籍贯
        String nativePlace = JSON.parseObject(params).getString("nativePlace");
        List<UserMatch> userMatchList = userMatchService.findUserByNativeAndRegion(position,nativePlace);
        return new Result(true,"查询成功",userMatchList);
    }

    @RequestMapping(value = "/searchUserMatch/{condition}",method = RequestMethod.GET)
    public Result searchGroup(@PathVariable String condition){
        List<UserMatch> userMatchList = userMatchService.searchGroupByCondition(condition);
        return new Result(true,"查询成功",userMatchList);
    }

    @RequestMapping(value = "/findUserMatchById",method = RequestMethod.GET)
    public Result searchGroupById(@RequestParam("id") String id){
        UserMatch UserMatch = userMatchService.searchUserMatchById(id);
        return new Result(true,"查询成功",UserMatch);
    }

    @RequestMapping(value = "/postUserMatch",method = RequestMethod.POST)
    public Result postUserMatch(@RequestBody String cartData){
        UserMatch userMatch = new UserMatch();
        userMatch.setNickname(JSON.parseObject(cartData).getString("name"));
        userMatch.setHeadPortrait(JSON.parseObject(cartData).getString("avatar"));
        userMatch.setIntroduce(JSON.parseObject(cartData).getString("des"));
        List<String> positionNow = JSON.parseArray(JSON.parseObject(cartData).getString("positionNow"), String.class);
        String position1 = positionNow.get(0)+"/"+positionNow.get(1)+"/"+positionNow.get(2);
        userMatch.setPosition(position1);
        List<String> positionOld = JSON.parseArray(JSON.parseObject(cartData).getString("positionOld"), String.class);
        String position2 = positionOld.get(0)+"/"+positionOld.get(1)+"/"+positionOld.get(2);
        userMatch.setNativePlace(position2);
        userMatch.setQrcode(JSON.parseObject(cartData).getString("qrCode"));
        userMatch.setWxnumber(JSON.parseObject(cartData).getString("wxnumber"));
        //System.out.println(group);
        int count = userMatchService.postUserMatch(userMatch);
        if(count==1){
            return new Result(true,"发布成功",null);
        }else {
            return new Result(false,"发布失败",null);
        }
    }



    /**
     * 上传图片至七牛云
     * @param file file
     * @return result
     */
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public Result post2QiuNiu(@RequestParam("image") MultipartFile file){
        //原始文件名称比如 aa.png
        String originalFilename = file.getOriginalFilename() ;
        //将原始名称修改为：唯一文件名称
//        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        String fileName = StringUtil.getRandomImgName(originalFilename);
        //System.out.println(fileName);
        //上传文件，上传到哪呢？图片服务器七牛云
        //把图片发放到距离图片最近的服务器上，降低我们自身服务器的带宽消耗
        String name = QiniuUtil.upload2Qiniu(file, fileName);
        //System.out.println(name);
        return new Result(true,"上传图片成功",name);
    }
}
